package com.apes.hr.paySupport.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.hr.base.model.Employee;
import com.apes.hr.base.model.Job;
import com.apes.hr.base.model.WorkPost;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Branch;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * @Title: SupportApplicationItem
 * @Description:付费支援申请单（支援人员明细）
 * @author: ZhengZeXian
 * @create: 2021/4/15 10:09
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SupportApplicationItem.CONST_TABLE_NAME)
public class SupportApplicationItem extends BaseModel {

    public static final String CONST_TABLE_NAME = "hr_support_application_item";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "付费支援申请单人员明细编码", padding = 15)
    @Comment("编码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "support_uid", referencedColumnName = "id")
    @Comment("人员编码")
    private Employee supportUid;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_id", referencedColumnName = "id")
    @Comment("支援前部门")
    private Branch branch;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_operation_id", referencedColumnName = "id")
    @Comment("支援前运营模式")
    private Enumeration branchOperation;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_id_after", referencedColumnName = "id")
    @Comment("支援后部门")
    private Branch branchAfter;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_after_operation_id", referencedColumnName = "id")
    @Comment("支援后运营模式")
    private Enumeration branchAfterOperation;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_fn_tag_id", referencedColumnName = "id")
    @Comment("赋能标签")
    private Enumeration branchFnTag;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "work_post_id", referencedColumnName = "id")
    @Comment("调动前岗位")
    private WorkPost workPost;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "work_post_id_after", referencedColumnName = "id")
    @Comment("调动后岗位")
    private WorkPost workPostAfter;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "job_id_after", referencedColumnName = "id")
    @Comment("调动后职位")
    private Job jobAfter;

    @Column(name = "driver")
    @Comment("是否准驾人员")
    private boolean driver;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "transfer_kind", referencedColumnName = "id")
    @Comment("支援性质")
    private Enumeration hrTransferKind;    //开店支援，同城支援，出差支援，开店出差支援

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Column(name = "start_date")
    @Comment("支援开始日期")
    private Date startDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "start_time",referencedColumnName = "id")
    @Comment("支援开始时间")
    private Enumeration startTime; // AM为上午 PM为下午

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Column(name = "end_date")
    @Comment("支援结束日期")
    private Date endDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "end_time",referencedColumnName = "id")
    @Comment("支援结束时间")
    private Enumeration endTime; // AM为上午 PM为下午

    @Column(name = "support_days", length = 50, nullable = false)
    @Comment("支援天数")
    private String supportDays;

//    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
//    @Column(name = "actual_end_date")
//    @Comment("实际最后支援日期")
//    private Date actualEndDate;
//
//    @States(name = "支援结束时间", values = {
//            @State(id = "AM", name = "上午"),
//            @State(id = "PM", name = "下午"),
//    })
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "actual_end_time", nullable = false, referencedColumnName = "id")
//    @Comment("实际最后支援结束时间")
//    private BusinessAttributeValue actualEndTime;

    @Column(name = "toll_case",columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("收费情况标识")
    private boolean tollCase = false;   //1、免费，2、收费

    @Column(name = "toll_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("收费金额标准/天")
    private double tollAmt = 0;

    @Column(name = "discounts_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("实际收费金额/天")
    private double discountsAmt = 0;

//    @Column(name = "toll_total_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
//    @Comment("总计收费金额")
//    private double tollTotalAmt = 0;

//    @Column(name = "discounts_total_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
//    @Comment("总计优惠金额")
//    private double discountsTotalAmt = 0;
//
    @Column(name = "actual_pay_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("实际需支付总金额")
    private double actualPayAmt = 0;

    @Column(name = "icon_execute", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否已执行")
    private boolean iconExecute = false;

    @Column(name = "repeal", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否已终止")
    private boolean repeal = false;

    @Column(name = "role_list", length = 250)
    @Comment("本地角色备份")
    private String roleList;

    @Column(name = "candidates", length = 4000)
    @Comment("出人门店店长OA候选人")
    private String candidates;

    @ManyToOne(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
    @JoinColumn(name = "supportApplication_id", referencedColumnName = "id",nullable = false)
    private SupportApplication supportApplication;

    @OneToMany(mappedBy="supportApplicationItem",cascade=CascadeType.ALL)
    private List<SupportApplicationRole> supportApplicationRoles;

    public List<SupportApplicationRole> getSupportApplicationRoles() {
        if (supportApplicationRoles == null) return null;
        for (SupportApplicationRole role : supportApplicationRoles) {
            role.setSupportApplicationItem(this);
        }
        return supportApplicationRoles;
    }

    @OneToMany(mappedBy="supportApplicationItem",cascade=CascadeType.ALL)
    private List<SupportApplicationAffirm> supportApplicationAffirms;

    public List<SupportApplicationAffirm> getSupportApplicationAffirms() {
        if (supportApplicationAffirms == null) return null;
        for (SupportApplicationAffirm affirm : supportApplicationAffirms) {
            affirm.setSupportApplicationItem(this);
        }
        return supportApplicationAffirms;
    }
}
